package io.renren.modules.tms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.excel.EasyExcelUtil;
import io.renren.common.utils.excel.ResponseMsg;
import io.renren.common.utils.excel.ResponseMsgUtil;
import io.renren.config.NoToken;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.job.task.tms.ShunDaShipmentTask;
import io.renren.modules.tms.entity.ShipmentError;
import io.renren.modules.tms.service.ShipmentService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/tms")
public class TmsController {

    @Resource
    private ShipmentService shipmentService;

    @Resource
    private ShunDaShipmentTask shunDaShipmentTask;

    @ApiOperation(value = "下载模板", notes = "下载模板")
    @PostMapping("/downloadTemplate")
    public void downloadImportTemplate(HttpServletResponse response, HttpServletRequest request) {
        EasyExcelUtil.downloadImportTemplate(response, request, "TMS数变更数据模板.xlsx");
    }

    @ApiOperation(value = "导入数据")
    @PostMapping("/importExcel")
    @DataSource("tms")
    public ResponseMsg importExcel(@RequestParam("file") MultipartFile file, int type) {
        return shipmentService.importExcel(file, type);
    }

    @ApiOperation(value = "返回异常报表")
    @GetMapping("/errorReport")
    @DataSource("tms")
    @NoToken
    public ResponseMsg errorReport(@RequestParam Map<String, Object> params) {
//        String startDate = "2024-03-01";
//        params.put("insertDate_begin", startDate);
        String startDate = (String) params.get("insertDate_begin");
//        String endDate = "2024-04-01";
//        params.put("insertDate_end", endDate);
        String endDate = (String) params.get("insertDate_end");
        if (StringUtils.isBlank(startDate) || StringUtils.isBlank(endDate)){
            return ResponseMsgUtil.error("起止时间未选择！");
        }
        return ResponseMsgUtil.success(shunDaShipmentTask.getErrorShipment(params));
    }

}
